package com.xpgk.xpsoft.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

//用户权限控制
public class XPSoftRight {
	static private Map<String, Integer> rightMap = new HashMap<>();

	static public boolean LoadRights() {
		List<Record> list = Db.find("select ID,URL from FRM_RIGHT");
		List<Record> list2 = new ArrayList<Record>();
		for (Iterator<Record> iterator = list.iterator(); iterator.hasNext();) {
			Record record = (Record) iterator.next();
			String url = record.getStr("url");
			if (url != null && url.toLowerCase().endsWith("_ui")) {
				url = url.substring(0, url.length() - 3);
				list2.add(new Record().set("url", url).set("id", record.getInt("id")));
			}
		}
		list.addAll(list2);
		for (Iterator<Record> iterator = list.iterator(); iterator.hasNext();) {
			Record record = (Record) iterator.next();
			String url = record.getStr("url");
			if (url != null)
				rightMap.put(url, record.getInt("id"));
		}
		return true;
	}

	static public boolean hasAction(String actionKey) {
		if (rightMap.isEmpty())
			XPSoftRight.LoadRights();
		return rightMap.containsKey(actionKey);
	}

	static public Integer getRightID(String actionKey) {
		if (rightMap.isEmpty())
			XPSoftRight.LoadRights();
		return rightMap.get(actionKey);
	}
}
